package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.admin.entity.StartSysDictionaryItem;
import com.start.admin.service.StartSysDictionaryItemService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.common.utils.TreeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/start/sysDictionaryItem")
public class StartSysDictionaryItemController {

    @Autowired
    private StartSysDictionaryItemService sysDictionaryService;

    @GetMapping("/page")
    public Result page(Page page, StartSysDictionaryItem entity) {
        LambdaQueryWrapper<StartSysDictionaryItem> lambdaQuery = DataQueryConstructor.createEntity(entity).lambda();
        lambdaQuery.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(sysDictionaryService.page(page, lambdaQuery));
    }

    @GetMapping("/listByDictionaryCode")
    public Result<?> listByDictionaryCode(String code) {
        List<StartSysDictionaryItem> list = sysDictionaryService.listByDictionaryCode(code);
        return Result.ok(TreeModelUtils.createTree(list));
    }

    @GetMapping("/listByDictionaryDictionaryId")
    public Result<?> listByDictionaryDictionaryId(String dictionaryId) {
        List<StartSysDictionaryItem> list = sysDictionaryService.listByDictionaryId(dictionaryId);
        return Result.ok(TreeModelUtils.createTree(list));
    }

    @PostMapping("/add")
    public Result add(@RequestBody StartSysDictionaryItem entity) {
        sysDictionaryService.saveDictionaryItem(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    public Result updateById(@RequestBody StartSysDictionaryItem entity) {
        sysDictionaryService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") String id) {
        return Result.ok(sysDictionaryService.getById(id));
    }


    @DeleteMapping("deleteById/{id}")
    public Result<?> deleteById(@PathVariable("id") String id) {
        sysDictionaryService.removeById(id);
        return Result.ok();
    }
}
